package cn.phecda.boot.web.openapi.configuration;

import cn.phecda.boot.web.core.annotation.ConditionalOnSwaggerEnabled;
import cn.phecda.boot.web.core.properties.SwaggerProperties;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
import io.swagger.v3.oas.annotations.security.OAuthFlow;
import io.swagger.v3.oas.annotations.security.OAuthFlows;
import io.swagger.v3.oas.annotations.security.OAuthScope;
import io.swagger.v3.oas.annotations.security.SecurityScheme;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p> Springdoc OpenApi配置信息 </p>
 *
 * @author : wanpinwei
 */
@Slf4j
@ConditionalOnSwaggerEnabled
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(SwaggerProperties.class)
@SecurityScheme(name = "security_auth", type = SecuritySchemeType.OAUTH2,
        flows = @OAuthFlows(authorizationCode = @OAuthFlow(
                authorizationUrl = "${springdoc.oAuthFlow.authorizationUrl}"
                , tokenUrl = "${springdoc.oAuthFlow.tokenUrl}", scopes = {
                @OAuthScope(name = "springdoc.read", description = "read scope"),
                @OAuthScope(name = "springdoc.write", description = "write scope") })))
public class SpringdocConfiguration {
    @PostConstruct
    public void postConstruct() {
        log.info("|Phecda Boot Web|- Springdoc Configuration");
    }

    @Bean
    @ConditionalOnMissingBean
    public OpenAPI createOpenApi() {
        return new OpenAPI()
                .info(new Info().title("Phecda Cloud")
                        .description("Phecda Cloud Microservices Architecture")
                        .version("Swagger V3")
                        .license(new License().name("Apache 2.0").url("http://www.apache.org/licenses/")))
                .externalDocs(new ExternalDocumentation()
                        .description("Phecda Cloud Documentation")
                        .url(" https://www.phecda.cn"));
    }
}
